﻿#region
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Linq;
using Y2M2.Properties;

#endregion

namespace Y2M2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Settings.Default.DownloadRoot))
                Settings.Default.DownloadRoot = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            Settings.Default.AudioOnly = true;

            ClipboardMonitor.Start();
            ClipboardMonitor.OnClipboardChange += ClipboardMonitor_OnClipboardChange;
        }

        private void OnClosing(object sender, CancelEventArgs e)
        {
            ClipboardMonitor.Stop();

            Settings.Default.Save();
        }

        private void ClipboardMonitor_OnClipboardChange(ClipboardFormat format, object data)
        {
            string url = data as string;

            if (url == null)
                return;

            Dispatcher.Invoke(new Action<string>(x => ShowUrl(x)), url);
        }

        private void ShowUrl(string url)
        {
            ClipControl clipControl = new ClipControl(url);
            pnlClip.Children.Add(clipControl);

            scvScroll.ScrollToBottom();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            for (int i = pnlClip.Children.Count - 1; i >= 0; i--)
                if (((ClipControl)pnlClip.Children[i]).Finished)
                    pnlClip.Children.RemoveAt(i);
        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("http://kimgwajang.tistory.com/266");
        }

        private void uscSetting_TopMostChanged(object sender, SettingControl.TopMostChangedEventArgs e)
        {
            Topmost = e.HasChecked;
        }
    }
}